package cn.sinata.wanglin.mine.activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.adapter.MyrepairAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Myrepair;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

/**
 * 我的报修列表界面
 */
public class MyrepairActivity extends RecyclerListActivity {
    private MyrepairAdapter mMyrepairAdapter;
    private List<Myrepair> mDatas=new ArrayList<Myrepair>();
    private int pageNo = 0;
    @Override
    protected void initView() {
        super.initView();
        setTitle("我的报修");
        showDialog();
        getData();

    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        mMyrepairAdapter=new MyrepairAdapter(mDatas);
        mMyrepairAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(mContext).go(MyrepairdetailActivity.class).put("myrepair",mDatas.get(position).getId())
                        .start();
            }
        });
        return mMyrepairAdapter;
    }

    @Override
    protected void pullDownRefresh() {
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        pageNo++;
        getData();
    }
    void getData(){
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getRepairList(userid,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<Myrepair>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<Myrepair> datas) {
                        if (pageNo == 0) {
                            mDatas.clear();
                        }
                        if (datas.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mDatas.addAll(datas);
                        mMyrepairAdapter.notifyDataSetChanged();
                    }
                });

    }
}
